package DEBUGGING;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// ***************************************************************************
// *** CLASS: DEBUG
// ***************************************************************************
public class DEBUG
{
  private static boolean __debug=true; //false;
  private static boolean listing=true;
  private static Button onOffButton=new Button();
  private static Button detailedButton=new Button();
  private static TracePanel tracePanel=new TracePanel();

  
  public static TracePanel getTracePanel() { return(tracePanel); }
  public static boolean getDebug() { return __debug; }
  public static void setDebug(boolean value) { __debug=value; tracePanel.updateButtons(); }
  public static void INFO(String s) { println(s); }
  public static void TRACE(String s) { if(__debug) println(s); }
  public static void ERROR(String s) { println("ERROR: "+s); }
  public static void NOT_IMPLEMENTED(String s) { println("NOT IMPLEMENTED: "+s); }
  public static void EXCEPTION(String s,Exception e) { println(s+e); }

  public static void ASSERT(boolean cond) { }
  
  private static void println(String msg)
  { if(listing)
    {
//    msg="Thread("+Thread.currentThread().getName()+") "+msg;
      System.out.println(msg);
	  tracePanel.addLine(msg);
    }  
  }  



  
  // ********************************************************************
  // *** INNER CLASS: TracePanel
  // ********************************************************************
  public static class TracePanel extends Panel
  {
    java.awt.List list=new java.awt.List();
	
    
    TracePanel()
    { setName("Trace Outputs");
      setLayout(new BorderLayout());
      this.add(list,BorderLayout.CENTER);
	  
	  // Set up control panel
      Panel controlPanel=new Panel();
      this.add(controlPanel,BorderLayout.NORTH);
	  
	  // Add Stop/Resume button to control panel
      onOffButton.addActionListener(new ActionListener() 
      { public void actionPerformed(ActionEvent e) 
        { println("----- BREAK (lines skipped)");
		      listing=!listing;
          updateButtons();
        };	
      }); 
      controlPanel.add(onOffButton);
      
      // Add Detailed/Breef button to control panel
      detailedButton.addActionListener(new ActionListener() 
      { public void actionPerformed(ActionEvent e) 
        { __debug=!__debug;
          updateButtons();
        };	
      }); 
      controlPanel.add(detailedButton);
      
      // Add Clear button to control panel
      final Button clearButton=new Button("Clear");
      clearButton.addActionListener(new ActionListener() 
      { public void actionPerformed(ActionEvent e) 
        { list.removeAll(); };	
      }); 
      controlPanel.add(clearButton);
	    updateButtons();
    }
    
    private void updateButtons()
    { if(listing) onOffButton.setLabel("Stop Listing");
      else        onOffButton.setLabel("Resume");  
      if(__debug) detailedButton.setLabel("Brief Listing");
      else        detailedButton.setLabel("More Details");  
    }  
    

    public void addLine(String line)
    {
      int cnt=list.getItemCount();
      list.add(line);
      list.makeVisible(cnt);	
    }    
  }



}      